package com.bei_en.teacher.admin.controller.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionStatus;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.user.bean.UserFinance;
import com.bei_en.teacher.admin.model.user.condition.AddUserFinanceCondition;
import com.bei_en.teacher.admin.model.user.condition.UserFinanceCondition;
import com.bei_en.teacher.admin.model.user.vo.UserFinanceVo;
import com.bei_en.teacher.admin.model.users.vo.UsersDetailsVo;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.service.user.UserFinanceService;
import com.bei_en.teacher.admin.service.users.UsersDetailsService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.util.interceptor.OAuthRequired;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * 
 * @author Guocg
 * @date 2016-10-2 19:23:07
 */
@Controller
@RequestMapping("/api/UserFinance")
public class UserFinanceAPPController extends BaseController {
	@Autowired
	UsersDetailsService usersDetailsService;
	@Autowired
	UserFinanceService userFinanceService;

	@RequestMapping("/currencyRule")
	@ResponseBody
	public JsonResult currencyRule(Model model, @Valid UserFinanceCondition vo,
			BindingResult br, HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {
			JSONObject object = null;
			JSONArray array = new JSONArray();
			object = new JSONObject();
			object.put("key", "1");
			object.put("value", "5");
			array.add(object);
			object = new JSONObject();
			object.put("key", "2");
			object.put("value", "5");
			array.add(object);
			object = new JSONObject();
			object.put("key", "3");
			object.put("value", "5");
			array.add(object);
			return new JsonSuccessResult(session.getId(), array);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/userFinance")
	@ResponseBody
	@OAuthRequired
	public JsonResult userFinance(Model model, @Valid UserFinanceCondition vo,
			BindingResult br, HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			UsersVo usersVo = getUser(session);
			List<UserFinanceVo> list = new ArrayList<UserFinanceVo>();
			vo.setUserId(usersVo.getUserId());

			list = userFinanceService.findUserFinancePage(vo);
			JSONArray array = new JSONArray();
			JSONObject object = null;
			UserFinanceVo userFinanceVo = null;
			for (int i = 0; i < list.size(); i++) {
				object = new JSONObject();
				userFinanceVo = list.get(i);
				if (userFinanceVo.getUserIds() != null
						&& userFinanceVo.getUserIds() != 0) {
					UsersDetailsVo users = usersDetailsService
							.findById(userFinanceVo.getUserIds());
					seetingJsonObject(object, "nickname", users.getNickname());
				} else {
					seetingJsonObject(object, "nickname", "");
				}
				seetingJsonObject(object, "money", userFinanceVo.getMoney());
				seetingJsonObject(object, "status", userFinanceVo.getStatus());
				seetingJsonObject(object, "note", userFinanceVo.getNote());
				seetingJsonObject(object, "createTime", userFinanceVo
						.getCreateTime().getTime());
				array.add(object);
			}
			return new JsonSuccessResult(session.getId(), array);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/userFinanceAdd")
	@ResponseBody
	@OAuthRequired
	public JsonResult userFinanceAdd(Model model,
			@Valid AddUserFinanceCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		// userFinance.setId(vo.getId());
		// userFinance.setUserId(vo.getUserId());
		// userFinance.setUserIds(vo.getUserIds());
		// userFinance.setOrderId(vo.getOrderId());
		// userFinance.setMoney(vo.getMoney());
		// userFinance.setType(vo.getType());
		// userFinance.setSource(vo.getSource());
		// userFinance.setStatus(vo.getStatus());
		// userFinance.setNote(vo.getNote());
		// userFinance.setPaymentNumber(vo.getPaymentNumber());
		// userFinance.setRemaining(vo.getRemaining());
		// userFinance.setCreateTime(vo.getCreateTime());
		// TransactionStatus tr = userFinanceService.getTransactionStatus();
		//
		// try {
		//
		// //生成打赏人减少
		//
		// UsersVo usersVo = getUser(session);
		// UserFinanceCondition condition = new UserFinanceCondition();
		// condition.setUserId(vo.getUserIds());
		// List<UserFinanceVo> userFinanceVos = userFinanceService
		// .findUserFinancePage(condition);
		//
		// UserFinanceVo userFinance = new UserFinanceVo();
		// userFinance.setUserId(vo.getUserIds());
		// userFinance.setUserIds(usersVo.getUserId());
		// userFinance.setMoney(vo.getMoney());
		// userFinance.setType(1);
		// userFinance.setSource(vo.getSource());
		// userFinance.setStatus(2);
		// userFinance.setCreateTime(new Date());
		//
		// if (!userFinanceVos.isEmpty()) {
		// userFinance.setRemaining(userFinanceVos.get(0)
		// .getRemaining() - vo.getMoney());
		//
		// } else {
		// userFinance.setRemaining(vo.getMoney());
		// }
		// UsersDetailsVo users = usersDetailsService.findById(vo.getUserIds());
		// if (users != null) {
		// userFinance.setNote("打赏他人(" + users.getNickname() + ")");
		// }
		// int id = userFinanceService.merge(userFinance);
		// if(id>0){
		// // 生成被打赏增加
		// userFinance = new UserFinanceVo();
		// userFinance.setUserId(usersVo.getUserId());
		// userFinance.setUserIds(vo.getUserIds());
		// userFinance.setMoney(vo.getMoney());
		// userFinance.setType(1);
		// userFinance.setSource(vo.getSource());
		// userFinance.setStatus(1);
		// userFinance.setCreateTime(new Date());
		// condition = new UserFinanceCondition();
		// condition.setUserId(vo.getUserIds());
		// userFinanceVos = userFinanceService
		// .findUserFinancePage(condition);
		// if (!userFinanceVos.isEmpty()) {
		// userFinance.setRemaining(userFinanceVos.get(0).getRemaining()
		// + vo.getMoney());
		//
		// } else {
		// userFinance.setRemaining(vo.getMoney());
		// }
		// users = usersDetailsService
		// .findById(vo.getUserIds());
		// if (users != null) {
		// userFinance.setNote("他人打赏(" + users.getNickname() + ")");
		// }
		// id = userFinanceService.merge(userFinance);
		// if(id>0){
		// userFinanceService.commit(tr);
		// }else{
		// userFinanceService.rollback(tr);
		// }
		//
		// }else{
		// userFinanceService.rollback(tr);
		// }
		// return new JsonSuccessResult(session.getId(), "成功");
		// } catch (Exception e) {
		// userFinanceService.rollback(tr);
		// e.printStackTrace();
		// return new JsonFailResult(session.getId(), 1, "后台报错",
		// e.getMessage());
		// }
		try {
			//
			UsersVo usersVo = getUser(session);
			UserFinanceCondition condition = new UserFinanceCondition();
			condition.setUserId(usersVo.getUserId());
			List<UserFinanceVo> userFinanceVos = userFinanceService
					.findUserFinancePage(condition);
			UserFinanceVo userFinance = new UserFinanceVo();
			userFinance.setUserId(usersVo.getUserId());
			userFinance.setUserIds(0);
			userFinance.setMoney(vo.getMoney());
			userFinance.setType(1);
			userFinance.setSource(vo.getSource());
			userFinance.setStatus(1);
			userFinance.setCreateTime(new Date());
			if (!userFinanceVos.isEmpty()) {
				userFinance.setRemaining(userFinanceVos.get(0).getRemaining()
						+ vo.getMoney());

			} else {
				userFinance.setRemaining(vo.getMoney());
			}
			switch (userFinance.getSource()) {
			case 1:
				userFinance.setNote("点赞");
				break;
			case 2:
				userFinance.setNote("分享");
				break;
			case 3:
				userFinance.setNote("收藏");
				break;
			case 4:
				userFinance.setNote("评论");
				break;
			}
			int id = userFinanceService.merge(userFinance);
			return new JsonSuccessResult(session.getId(), "成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}

	}

	@RequestMapping("/userFinanceEdit")
	public JsonResult userFinanceEdit(Model model,
			@Valid UserFinanceCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			UserFinanceVo userFinance = new UserFinanceVo();

			userFinanceService.updateSelective(userFinance);

			return new JsonSuccessResult(session.getId(), "成功");

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

}
